package com.ccl.iot.device;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import com.ccl.iot.App;
import com.ccl.iot.dvr.utils.Consts;
import com.ccl.iot.privates.BitmapBuilder;

/**
 * 意向控制中批处理的控制器
 */
public class Batch2IntentionControler extends DeviceControler{
	
	/*
	 * 	Static functions
	 */
	public static boolean RegisterControlerType(){
		return DeviceControler.RegisterControlerType(Batch2IntentionControler.class);
	}
	
	/*
	 * 	Non-static functions
	 */
	private BatchIntentionDevice mDevice;
	
	public Batch2IntentionControler(BatchIntentionDevice aDevice) {
		super(aDevice);
		mDevice = aDevice;
	}
	
	@Override
	protected boolean onTouchDown(float aX, float aY){
		onBitmapChange();
		return true;
	}
	
	@Override
	protected boolean onTouchMove(float aX, float aY){
		return true;
	}
	
	@Override
	protected boolean onTouchUp(float aX, float aY){
		if (mDevice != null){
			mDevice.getBatch().execute();
		}
		
		onBitmapChange();
		return true;
	}
	
	protected boolean onTouchCancel(float aX, float aY){
		onBitmapChange();
		return true;
	}
	
	@Override
	public Bitmap getBitmap(int aWidth, int aHeight){
		if (aWidth > 0 && aHeight > 0 && mDevice != null){
			Bitmap tBackground = Bitmap.createBitmap(aWidth, aHeight, Config.RGB_565);
			Canvas tCanvas = new Canvas(tBackground);
			Bitmap tBitmap = null;
			Rect tPadding = super.getPadding();
			int tWidth = Math.abs(aWidth - tPadding.left - tPadding.right);
			int tHeight = Math.abs(aHeight - tPadding.top - tPadding.bottom);

			if (mDevice.getImageCount() == 0){
				tCanvas.drawColor(super.getBackColor());
				tBitmap = BatchIconProvider.Build(mDevice.getBatch(),Color.WHITE,tWidth,tHeight);
			}else{
				tBitmap = BitmapBuilder.GetBitmap(mDevice.getImage(), tWidth, tHeight, false);
				tCanvas.drawColor(Color.WHITE);
			}
			
			if (tBitmap != null){
				tCanvas.drawBitmap(tBitmap, tPadding.top, tPadding.bottom, new Paint());
			}

			return tBackground;
		}
		
		return null;
	}

}
